package com.item.util;
import java.awt.Color;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;

import com.base.support.PropertySupport;

/**
 * 汇文系统图书馆验证码处理	2014-7-4
 * @author yjh
 *
 */

public class VerifyCodeCrackBlack {

	//读入用于匹配的图片切片
	//进行匹配的图片map,最早进行加载且只加载一次即可
	private static Map<BufferedImage, String> trainImgs;
	static{
		try{
			trainImgs = new HashMap<BufferedImage, String>();
			File dir = new File(PropertySupport.getProperty("file.path")+"verify/codetrain1");
			File[] files = dir.listFiles();
			for (File file : files) {
				trainImgs.put(ImageIO.read(file), file.getName().charAt(0) + "");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	private static int isBlack(int colorInt) {
		Color color = new Color(colorInt);
		if (color.getRed() == 4 && color.getGreen() == 2 && color.getBlue() == 4) {
			return 1;
		}
		return 0;
	}
	
	private static BufferedImage removeBackgroud(File picFile)
			throws Exception {
		BufferedImage img = ImageIO.read(picFile);
		int width = img.getWidth();
		int height = img.getHeight();
		for (int x = 0; x < width; ++x) {
			for (int y = 0; y < height; ++y) {
				if(isBlack(img.getRGB(x, y)) == 0){
					img.setRGB(x, y, Color.WHITE.getRGB());
				}
			}
		}
		return img;
	}
	private static List<BufferedImage> splitImage(BufferedImage img)
			throws Exception {
		List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
		int width = img.getWidth();
		int height = img.getHeight();
		List<Integer> weightlist = new ArrayList<Integer>();
		for (int x = 0; x < width; ++x) {
			int count = 0;
			for (int y = 0; y < height; ++y) {
				if (isBlack(img.getRGB(x, y)) == 1) {
					count++;
				}
			}
			weightlist.add(count);
		}
		for (int i = 0; i < weightlist.size(); i++) {
			int length = 0;
			while (weightlist.get(i) > 0) {
				i++;
				length++;
			}
			if (length > 12) {
				subImgs.add(removeBlank(img.getSubimage(i - length, 0,
						length / 2, height)));
				subImgs.add(removeBlank(img.getSubimage(i - length / 2, 0,
						length / 2, height)));
			} else if (length > 3) {
				subImgs.add(removeBlank(img.getSubimage(i - length, 0,
						length, height)));
			}
		}
		return subImgs;
	}
	private static BufferedImage removeBlank(BufferedImage img) throws Exception {
		int width = img.getWidth();
		int height = img.getHeight();
		int start = 0;
		int end = 0;
		Label1: for (int y = 0; y < height; ++y) {
			for (int x = 0; x < width; ++x) {
				if (isBlack(img.getRGB(x, y)) == 1) {
					start = y;
					break Label1;
				}
			}
		}
		Label2: for (int y = height - 1; y >= 0; --y) {
			for (int x = 0; x < width; ++x) {
				if (isBlack(img.getRGB(x, y)) == 1) {
					end = y;
					break Label2;
				}
			}
		}
		return img.getSubimage(0, start, width, end - start + 1);
	}
	
	private static String getSingleCharOcr(BufferedImage img,
			Map<BufferedImage, String> map) {
		String result = "";
		int width = img.getWidth();
		int height = img.getHeight();
		boolean flag;
		for(BufferedImage bi : map.keySet()){
			flag = true;
			if(bi.getWidth() != width || bi.getHeight() != height){
				continue ;
			}
			Label1: for (int x = 0; x < width; ++x) {
				for (int y = 0; y < height; ++y) {
					if (isBlack(img.getRGB(x, y)) != isBlack(bi.getRGB(x, y))){
						    flag = false;
							break Label1;
					}
				}
			}
			if(flag){
				result = map.get(bi);
				break;
			}
		}
		return result;
	}
	
	/**
	 * 读取所有颜色为红色的字符内容
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public static String getAllOcr(File file) throws Exception {
		BufferedImage img = removeBackgroud(file);
		List<BufferedImage> listImg = splitImage(img);
		String result = "";
		for (BufferedImage bi : listImg) {
			result += getSingleCharOcr(bi, trainImgs);
		}
		//删除文件
//		if(file.exists())
//			file.delete();
		return result;
	}

}
